package jdbc;

import java.sql.*;

/**
 * 手写jdbc增删改
 */
public class Demo1 {
    public static void main(String[] args) {

        String driver="com.mysql.cj.jdbc.Driver";
        //开启了手动提交事务
        String url="jdbc:mysql://localhost:3306/test02_library?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false&rewriteBatchedStatements=true";
        String user="root";
        String password="root";

        Connection connection = null;
        //Statement statement = null; 有sql注入问题
        PreparedStatement preparedStatement=null;
        ResultSet resultSet = null;

        try {
            //加载驱动
            Class.forName(driver);
            //创建链接
            connection = DriverManager.getConnection(url, user, password);
            //关闭事务自动提交,只影响当前connection
            connection.setAutoCommit(false);

            //查询
            String sqlQuery="select * from teacher where  id=?";
            //预编译并获取操作对象,
            preparedStatement = connection.prepareStatement(sqlQuery);
            //赋值并执行sql
            preparedStatement.setInt(1,3);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                System.out.print(resultSet.getInt(1)+" ");
                System.out.print(resultSet.getString(2)+" ");
                System.out.println(resultSet.getString(3));
            }

            //添加
            String sqlInsert="insert into teacher(name,hobby) values (?,?)";
            //预编译并获取操作对象,
            preparedStatement = connection.prepareStatement(sqlInsert);
            //赋值并执行sql
            preparedStatement.setString(1,"浩老师");
            preparedStatement.setString(2,"旅游");
            int i = preparedStatement.executeUpdate();
            System.out.println("添加"+i);

            //添加批处理
            preparedStatement = connection.prepareStatement(sqlInsert);
            for (int j = 1; j <=2000; j++) {
                preparedStatement.setString(1,"浩老师"+j);
                preparedStatement.setString(2,"旅游"+j);
                preparedStatement.addBatch();
                if (j%500==0){
                    preparedStatement.executeBatch();
                    preparedStatement.clearBatch();
                }
            }

            // 模拟异常
            //int a=10/0;

            //删除
            String sqlDelKey="delete from teacher_student where teacher_id=?";
            //预编译并获取操作对象,
            preparedStatement = connection.prepareStatement(sqlDelKey);
            //赋值并执行sql
            preparedStatement.setInt(1,2);
            int j = preparedStatement.executeUpdate();
            System.out.println("删除key"+j);
            String sqlDel="delete from teacher where id=?";
            //预编译并获取操作对象,
            preparedStatement = connection.prepareStatement(sqlDel);
            //赋值并执行sql
            preparedStatement.setInt(1,2);
            int k = preparedStatement.executeUpdate();
            System.out.println("删除teacher"+k);

            //修改
            String sqlUpdate="update teacher set hobby=? where id=?";
            //预编译并获取操作对象,
            preparedStatement = connection.prepareStatement(sqlUpdate);
            //赋值并执行sql
            preparedStatement.setString(1,"新的爱好");
            preparedStatement.setInt(2,5);
            int h = preparedStatement.executeUpdate();
            System.out.println("修改"+h);
            //事务提交
            connection.commit();
            connection.setAutoCommit(true);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                //事务回滚
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            try {
                if (connection!=null)
                    connection.close();
                if (preparedStatement!=null)
                    preparedStatement.close();
                if (resultSet!=null)
                    resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }
}
